package guinew.controller;

import guinew.model.MainViewModel;
import guinew.view.ExportView;
import guinew.view.MainView;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultMutableTreeNode;

public class MainViewController{
	private MainView view;
	private MainViewModel model;
	
	public MainViewController(){
		model = new MainViewModel();
		view = new MainView(this);
	}
	
	/*
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new MainViewController();
			}
		});
	}*/

	public void openXmlFileChooser() {
		JFileChooser fileChooser = new JFileChooser();
		
		fileChooser.setDialogTitle("Please choose a valid XML-File");
		fileChooser.setFileFilter(new FileFilter(){
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory();
			}
			public String getDescription() {
				return "XML-File(*.xml)";
			}			
		});
		
		int returnVal = fileChooser.showOpenDialog(view);	
		
		// build the tree using the input file
		if (returnVal == JFileChooser.APPROVE_OPTION){
			File xmlFile = fileChooser.getSelectedFile();
			boolean openFileResult = model.setXmlFile(xmlFile);
			if(!openFileResult){
				//no signature file has been found
				JOptionPane.showMessageDialog(view, "No signature found for file "+ xmlFile.getName() +".");
			}
		}	
		else if (returnVal == JFileChooser.CANCEL_OPTION){}
	}

	public MainViewModel getModel() {
		return model;
	}

	public boolean isNodeInRedactionQueue(DefaultMutableTreeNode selectedNode) {
		return model.isNodeInRedactionQueue(selectedNode);
	}

	public void addSelectedNodeToRedactionQueue() {
		model.addSelectedNodeToRedactionQueue();
		
	}

	public void setSelectedNode(DefaultMutableTreeNode selectedNode) {
		model.setSelectedNode(selectedNode);
	}

	public DefaultMutableTreeNode getSelectedNode() {
		return model.getSelectedNode();
	}

	public void removeFromRedactionQueue(DefaultMutableTreeNode selectedNode) {
		model.removeFromRedactionQueue(selectedNode);
	}

	public void redactButtonPressed() {
		String output = "";
		String [] options = {"Ok", "Cancel"};
		int returnVal = 0;
		ArrayList<DefaultMutableTreeNode> redactionQueue = model.getRedactionQueue();
	    for(DefaultMutableTreeNode node : model.getRedactionQueue()){
	    	output = output+node.toString()+"\n";
	    }	

	    if(!redactionQueue.isEmpty()){
	    	returnVal = JOptionPane.showOptionDialog(view, "The following nodes will be redacted:\n\n"+output, "Confirm Redaction", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
		    if (returnVal == JOptionPane.YES_OPTION){
		    	model.redact();
		    	ExportView ev = new ExportView(view);
				ev.run();
		    }
		    	
	    }else 
	    	JOptionPane.showMessageDialog(null, "Please select a node you wish to redact!", "Error", JOptionPane.ERROR_MESSAGE);
		
	}

	public void signButtonPressed() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setDialogTitle("Open Private Key File");
		fileChooser.setFileFilter(new FileFilter(){
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith(".der") || f.isDirectory();
			}
			public String getDescription() {
				return "Key-File(*.der)";
			}			
		});
		
		int returnVal = fileChooser.showOpenDialog(view);	
		
		// build the tree using the input file
		if (returnVal == JFileChooser.APPROVE_OPTION){
			File file = fileChooser.getSelectedFile();
			model.signDocument(file);
			JOptionPane.showMessageDialog(view, "Signature has been exported to " + 
					model.getFile().getAbsolutePath().substring(0, model.getFile().getAbsolutePath().length()-3) + "sig");
		}
		
	}

	public void verifyButtonPressed() {
		JFileChooser fileChooser = new JFileChooser();
		
		fileChooser.setFileFilter(new FileFilter(){
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith(".crt") || f.isDirectory();
			}
			public String getDescription() {
				return "Certificate-File(*.crt)";
			}			
		});
		
		int returnVal = fileChooser.showOpenDialog(view);	
		
		// build the tree using the input file
		if (returnVal == JFileChooser.APPROVE_OPTION){
			File file = fileChooser.getSelectedFile();
			model.verifyDocument(file);
		}
	}
	
}
